package cn.edu.sgu.www.mhxysy.service.fabao.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.fabao.FabaoLiangyiAttributeCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.fabao.FabaoLiangyiAttributeCategory;
import cn.edu.sgu.www.mhxysy.mapper.fabao.FabaoLiangyiAttributeCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.fabao.FabaoLiangyiAttributeCategoryPager;
import cn.edu.sgu.www.mhxysy.service.fabao.FabaoLiangyiAttributeCategoryService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class FabaoLiangyiAttributeCategoryServiceImpl implements FabaoLiangyiAttributeCategoryService {

    private final FabaoLiangyiAttributeCategoryMapper liangyiAttributeCategoryMapper;

    @Autowired
    public FabaoLiangyiAttributeCategoryServiceImpl(FabaoLiangyiAttributeCategoryMapper fabaoLiangyiAttributeCategoryMapper) {
        this.liangyiAttributeCategoryMapper = fabaoLiangyiAttributeCategoryMapper;
    }

    @Override
    public void insert(FabaoLiangyiAttributeCategoryDTO fabaoLiangyiAttributeCategoryDTO) {
        FabaoLiangyiAttributeCategory fabaoLiangyiAttributeCategory = toEntity(fabaoLiangyiAttributeCategoryDTO);

        fabaoLiangyiAttributeCategory.setId(null);

        liangyiAttributeCategoryMapper.insert(fabaoLiangyiAttributeCategory);
    }

    @Override
    public void updateById(FabaoLiangyiAttributeCategoryDTO fabaoLiangyiAttributeCategoryDTO) {
        ObjectUtils.requireNonNull(fabaoLiangyiAttributeCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        FabaoLiangyiAttributeCategory fabaoLiangyiAttributeCategory = toEntity(fabaoLiangyiAttributeCategoryDTO);

        liangyiAttributeCategoryMapper.updateById(fabaoLiangyiAttributeCategory);
    }

    @Override
    public FabaoLiangyiAttributeCategory selectById(Integer id) {
        return liangyiAttributeCategoryMapper.selectById(id);
    }

    @Override
    public List<FabaoLiangyiAttributeCategory> selectByFabaoId(Integer fabaoId) {
        if (fabaoId == null) {
            return new ArrayList<>();
        }

        QueryWrapper<FabaoLiangyiAttributeCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("fabao_id", fabaoId);

        return liangyiAttributeCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public Page<FabaoLiangyiAttributeCategory> selectByPage(FabaoLiangyiAttributeCategoryPager pager) {
        QueryWrapper<FabaoLiangyiAttributeCategory> queryWrapper = new QueryWrapper<>();
        Page<FabaoLiangyiAttributeCategory> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getFabaoId() != null,
                "fabao_id", pager.getFabaoId()
        );

        return liangyiAttributeCategoryMapper.selectPage(page, queryWrapper);
    }

    private FabaoLiangyiAttributeCategory toEntity(FabaoLiangyiAttributeCategoryDTO fabaoLiangyiAttributeCategoryDTO) {
        FabaoLiangyiAttributeCategory fabaoLiangyiAttributeCategory = new FabaoLiangyiAttributeCategory();

        fabaoLiangyiAttributeCategory.setId(fabaoLiangyiAttributeCategoryDTO.getId());
        fabaoLiangyiAttributeCategory.setMax(fabaoLiangyiAttributeCategoryDTO.getMax());
        fabaoLiangyiAttributeCategory.setNote(fabaoLiangyiAttributeCategoryDTO.getNote());
        fabaoLiangyiAttributeCategory.setType(fabaoLiangyiAttributeCategoryDTO.getType());
        fabaoLiangyiAttributeCategory.setEffect(fabaoLiangyiAttributeCategoryDTO.getEffect());
        fabaoLiangyiAttributeCategory.setFabaoId(fabaoLiangyiAttributeCategoryDTO.getFabaoId());
        fabaoLiangyiAttributeCategory.setIsPercent(fabaoLiangyiAttributeCategoryDTO.getIsPercent());
        fabaoLiangyiAttributeCategory.setPropertyId(fabaoLiangyiAttributeCategoryDTO.getPropertyId());
        fabaoLiangyiAttributeCategory.setRelateType(fabaoLiangyiAttributeCategoryDTO.getRelateType());
        fabaoLiangyiAttributeCategory.setRelateGrade(fabaoLiangyiAttributeCategoryDTO.getRelateGrade());

        return fabaoLiangyiAttributeCategory;
    }

}